<?php
namespace Views;
/**
 * File holds class for generating feedback view
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Feedback {
	private $_smarty;
	private $_service;
	
	public function __construct() {
		global $smarty;
		$this->_service = new \Services\Feedback();
		$this->_smarty = $smarty;
	}
	/**
	 * Method shows feedback form to user. Title is generated from attribute
	 * If no match is found, attribute will be used as name!
	 * @param var $business Business identifier name
	 * @return void
	 */
	public function viewForm($business) {
		global $lang;
		
		//map for beautiful titles
		$businessMap = array(
				'academicHostel' => 'Academic Hostel',
				'revalCafe' => 'Pärnu mnt Reval Café',
				'victoriaCenter' => 'Victoria Center',
				'revalCafe1' => 'Telliskivi Reval Café',
				'revalCafe2' => 'Müürivahe Reval Café',
				'revalCafe3' => 'Viimsi Reval Café'
		);
		
		//map with beautiful name, or use name from url as fallback
		if (isset($businessMap[$business])) {
			$businessName = $businessMap[$business];
		} else {
			$businessName = ucfirst($business);
		}
		
		$this->_smarty->assign('businessName', $businessName);
		$this->_smarty->assign('lang', $lang);
		$this->_smarty->display('feedbackForm.tpl');
	}
	
	/**
	 * Method stores feedback to database
	 * @param array $post HTML POST array
	 */
	public function saveForm($post) {
		if(isset($post) && count($post) > 0) {
			$feedback = new \Entities\Feedback();
			$feedback->setBusiness($post['business']);
			$feedback->setComment($post['comment']);			
			$feedback->setRating((int)$post['score']);
			//FIXME: do user storing too, then map feedback with user
			if (strlen($post['email']) > 0 || strlen($post['phone']) > 0) {
				$feedback->setUser($post['email'] . ', ' . $post['phone']);
			}
			$feedback->setTime(time());
			//TODO: display a page with appropriate message!
			if ($this->_service->save($feedback)) {
				exit ('Thank You!');
			} else {
				exit ('We had trouble storing Your feedback :(');
			}
		}
	}
}